package com.bluetoothprint;

import android.content.Context;
import android.text.TextUtils;

import com.commonslibrary.commons.utils.SharePreferenceUtil;
import com.google.gson.Gson;

import java.util.Map;
import java.util.Set;

/**
 * date        :  2016-03-30  11:38
 * author      :  Mickaecle gizthon
 * description :
 */

public class LocalCacheUtils {
    private static LocalCacheUtils INSTANCE;
    private SharePreferenceUtil preferenceUtil;
    private final String LOGIN_INFO_KEY = "u";//注意写的越不好记越好
    private final String USER_INFO_KEY = "u1";//注意写的越不好记越好
    private final String LOCATION_KEY = "l";//注意写的越不好记越好
    private String KEYWORD_KEY = "k";//注意写的越不好记越好
    private String TMP_RECHARGE_KEY = "t";//注意写的越不好记越好
    private String HISTORY_TAB_KEY = "h";//注意写的越不好记越好
    private String LAST_TIME_USERNAME = "LU";
    private final String JPUSH_STATE = "JS";

    private String userId = "";
    private String userName = "";
    private String securityKey = "";
    private Context context;

    private String SHOW_GUIDE = "show_guid";

    private LocalCacheUtils() {
    }

    public void init(Context context) {
        this.context = context.getApplicationContext();
        this.preferenceUtil = SharePreferenceUtil.getInstance(context.getApplicationContext());
    }

    public static LocalCacheUtils getInstance() {
        if (INSTANCE == null) {
            INSTANCE = new LocalCacheUtils();
        }
        return INSTANCE;
    }

    public void clearAllSPData() {
       clearUserInfo();

        Map<String, ?> result = preferenceUtil.preferences.getAll();
        Set<String> resultSet = result.keySet();
        for (String key : resultSet) {
            preferenceUtil.setStringValue(key, null);
        }
    }

    public void clearUserInfo() {
        userId = "";
        userName = "";
        securityKey = "";
        preferenceUtil.setStringValue(USER_INFO_KEY, "");
    }
    public void setTabCategory(String firstCategory) {
        preferenceUtil.setStringValue(HISTORY_TAB_KEY, firstCategory);
    }

    public String getSecurityKey() {

        try {
            if (getLoginInfo() != null && !TextUtils.isEmpty(getLoginInfo().getSecurityKey())) {
                securityKey = getLoginInfo().getSecurityKey();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (TextUtils.isEmpty(securityKey)) {
            securityKey = "txsccl8457210%#&@4";
        }
        return securityKey;
    }


    public String getUserId() {
        if (TextUtils.isEmpty(userId)) {
            try {
                userId = getLoginInfo().getTUserID();
            } catch (Exception e) {
            }

        }
        return userId;
    }

    /**
     * 获取登录用户信息
     *
     * @return 登录用户信息
     */
    public UserInfoBean getLoginInfo() {
        String userInfo = preferenceUtil.getStringValue(USER_INFO_KEY);
        return new Gson().fromJson(userInfo, UserInfoBean.class);
    }

    /**
     * 保存用户信息
     *
     * @param userInfo 用户实体类
     */
    public void saveUserInfo(UserInfoBean userInfo) {
        String json = new Gson().toJson(userInfo);
        preferenceUtil.setStringValue(USER_INFO_KEY, json);
    }

    /**
     * 保存是否打开JPUSH到本地
     */
    public void saveIsOpenJPush(boolean isOpen) {
        UserInfoBean userInfoBean = getLoginInfo();
        if (isOpen)
            userInfoBean.setIsReceiveMessage(1);
        else
            userInfoBean.setIsReceiveMessage(0);
        saveUserInfo(userInfoBean);
    }


    /**
     * 得到是否开启JPUSH
     */
    public boolean getIsOpenJPush() {
        boolean isOpen = preferenceUtil.getBooleanValue(JPUSH_STATE);
        return isOpen;
    }
    public boolean isNeedShowGuide() {
        boolean isNeedShow  =  preferenceUtil.getBooleanValue(SHOW_GUIDE,true);
        if (isNeedShow){
            setNeedShowGuide(false);
        }
        return isNeedShow;
    }

    public void setNeedShowGuide(boolean isNeed){
        preferenceUtil.setBooleanValue(SHOW_GUIDE,isNeed);
    }


    //打印机最多中文字符
    private String PRINT_SIZE = "";
    public int getPrintMaxByte(){
      return   preferenceUtil.getIntValue(PRINT_SIZE,32);
    }

    public void setPrintMaxByte(int size){
        preferenceUtil.setIntValue(PRINT_SIZE,size);
    }
}
